package cn.virens.common.datasource.annotations.handler;

import cn.virens.common.datasource.DataSourceType;
import cn.virens.common.datasource.DataSourceUtils;
import cn.virens.common.spring.annotations.VirAbstractMethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.core.annotation.AnnotationAttributes;

public class DynamicDataSourceMethodInterceptor extends VirAbstractMethodInterceptor {

    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        AnnotationAttributes attrs = findAnnotationAttributes(invocation);
        if (attrs == null || !attrs.getBoolean("readOnly")) {
            DataSourceUtils.set0(DataSourceType.MASTER);
        } else {
            DataSourceUtils.set0(DataSourceType.SLAVE);
        }

        return invocation.proceed();
    }

}
